<?php

/**
 * 职位控制器
 * User: wangchao
 * Date: 2017/10/17 0017
 * Time: 8:19
 */

namespace Admin\Controller;

class PositionController extends CommonController
{
    /**
     * 公司列表
     */
    public function company_list()
    {
        //echo session('admin_id');exit;
        $list = $this->lists('Company', 'status = 1', 'id desc');
        $this->assign('list', $list);
        $this->display('company_list');
    }

    /**
     * 添加公司
     */
    public function add_company()
    {
        if (IS_POST) {
            $name = I('post.name');
            $data = [
                'name' => $name,
                'status' => 1,
                'create_time' => date('Y-m-d H:i:s', time()),
            ];
            $res = M('Company')->add($data);
            if (empty($res)) {
                $this->error('服务器繁忙，请稍后再试！');
            }
            $this->success('添加成功', U('Position/company_list'));
        }
        $this->display('add_company');
    }

    /**
     * 编辑公司
     */
    public function edit_company()
    {
        if (IS_POST) {
            $id = I('post.id');
            $name = I('post.name');
            $data = [
                'name' => $name,
            ];
            $res = M('Company')->where(['id' => $id])->save($data);
            if (empty($res)) {
                $this->error('保存数据出错。');
            }
            $this->success('保存成功。', U('Position/company_list'));
        }
        $id = I('id');
        $info = M('Company')->where(['id' => $id])->find();
        $this->assign('info', $info);
        $this->display('edit_company');
    }

    /**
     * 删除公司
     */
    public function del_company()
    {
        $id = I('id');
        M('Company')->where(['id' => $id])->setField('status', 0);
        $this->success('删除成功', U('Position/company_list'));
    }

    /**
     * 部门列表
     */
    public function part_list()
    {
        $list = $this->lists('Part', 'status = 1', 'id desc');
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['company_name'] = M('Company')->where(['id'=>$v['company_id']])->getField('name');
            }
        }
        $this->assign('list', $list);
        $this->display('part_list');
    }

    /**
     * 添加部门
     */
    public function add_part()
    {
        if (IS_POST) {
            $name = I('post.name');
            $company_id = I('post.company_id');
            $data = [
                'name' => $name,
                'company_id' => $company_id,
                'status' => 1,
                'create_time' => date('Y-m-d H:i:s', time()),
            ];
            $res = M('Part')->add($data);
            if (empty($res)) {
                $this->error('服务器繁忙，请稍后再试！');
            }
            $this->success('添加成功', U('Position/part_list'));
        }
        $company = M('Company')->where(['status' => 1])->select();
        $this->assign('company', $company);
        $this->display('add_part');
    }

    /**
     * 编辑部门
     */
    public function edit_part()
    {
        if (IS_POST) {
            $id = I('post.id');
            $name = I('post.name');
            $data = [
                'name' => $name,
            ];
            $res = M('Part')->where(['id' => $id])->save($data);
            if (empty($res)) {
                $this->error('保存数据出错。');
            }
            $this->success('保存成功。', U('Position/part_list'));
        }
        $id = I('id');
        $info = M('Part')->where(['id' => $id])->find();
        $company = M('Company')->where(['status' => 1])->select();
        $this->assign('info', $info);
        $this->assign('company',$company);
        $this->display('edit_part');
    }

    /**
     * 删除部门
     */
    public function del_part()
    {
        $id = I('id');
        M('Part')->where(['id' => $id])->setField('status', 0);
        $this->success('删除成功', U('Position/part_list'));
    }

    /**
     * 部门列表
     */
    public function workshop_list()
    {
        $list = $this->lists('Workshop', 'status = 1', 'id desc');
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['part_name'] = M('Part')->where(['id' => $v['part_id']])->getField('name');
            }
        }
        $this->assign('list', $list);

        $Data = M('Workshop'); // 实例化Data数据模型
        $count   = $Data->where( 'status = 1')->count();
        $p = getpage($count,20);
        $this->assign('page', $p->show()); // 赋值分页输出
        $this->display('workshop_list');
    }

    /**
     * 添加部门
     */
    public function add_workshop()
    {
        if (IS_POST) {
            $name = I('post.name');
            $part_id = I('post.part_id');
            $data = [
                'name' => $name,
                'part_id' => $part_id,
                'status' => 1,
                'create_time' => date('Y-m-d H:i:s', time()),
            ];
            $res = M('Workshop')->add($data);
            if (empty($res)) {
                $this->error('服务器繁忙，请稍后再试！');
            }
            $this->success('添加成功', U('Position/workshop_list'));
        }
        $part = M('Part')->where(['status' => 1])->select();
        $this->assign('part', $part);
        $this->display('add_workshop');
    }

    /**
     * 编辑部门
     */
    public function edit_workshop()
    {
        if (IS_POST) {
            $id = I('post.id');
            $name = I('post.name');
            $data = [
                'name' => $name,
            ];
            $res = M('Workshop')->where(['id' => $id])->save($data);
            if (empty($res)) {
                $this->error('保存数据出错。');
            }
            $this->success('保存成功。', U('Position/workshop_list'));
        }
        $id = I('id');
        $info = M('Workshop')->where(['id' => $id])->find();
        $part = M('Part')->where(['status' => 1])->select();
        $this->assign('part', $part);
        $this->assign('info', $info);
        $this->display('edit_workshop');
    }

    /**
     * 删除部门
     */
    public function del_workshop()
    {
        $id = I('id');
        M('Workshop')->where(['id' => $id])->setField('status', 0);
        $this->success('删除成功', U('Position/workshop_list'));
    }

    /**
     * 部门列表
     */
    public function group_list()
    {
        $list = $this->lists('Group', 'status = 1', 'id desc');
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['workshop_name'] = M('Workshop')->where(['id' => $v['workshop_id']])->getField('name');
            }
        }
        $this->assign('list', $list);

        $Data = M('Group'); // 实例化Data数据模型
        $count   = $Data->where( 'status = 1')->count();
        $p = getpage($count,20);
        $this->assign('page', $p->show()); // 赋值分页输出

        $this->display('group_list');
    }

    /**
     * 添加部门
     */
    public function add_group()
    {
        if (IS_POST) {
            $name = I('post.name');
            $workshop_id = I('post.workshop_id');
            $data = [
                'name' => $name,
                'workshop_id' => $workshop_id,
                'status' => 1,
                'create_time' => date('Y-m-d H:i:s', time()),
            ];
            $res = M('Group')->add($data);
            if (empty($res)) {
                $this->error('服务器繁忙，请稍后再试！');
            }
            $this->success('添加成功', U('Position/group_list'));
        }
        $workshop = M('Workshop')->where(['status' => 1])->select();
        $this->assign('workshop', $workshop);
        $this->display('add_group');
    }

    /**
     * 编辑部门
     */
    public function edit_group()
    {
        if (IS_POST) {
            $id = I('post.id');
            $name = I('post.name');
            $data = [
                'name' => $name,
            ];
            $res = M('Group')->where(['id' => $id])->save($data);
            if (empty($res)) {
                $this->error('保存数据出错。');
            }
            $this->success('保存成功。', U('Position/group_list'));
        }
        $id = I('id');
        $info = M('Group')->where(['id' => $id])->find();
        $workshop = M('Workshop')->where(['status' => 1])->select();
        $this->assign('workshop', $workshop);
        $this->assign('info', $info);
        $this->display('edit_group');
    }

    /**
     * 删除部门
     */
    public function del_group()
    {
        $id = I('id');
        M('Group')->where(['id' => $id])->setField('status', 0);
        $this->success('删除成功', U('Position/group_list'));
    }
}
